﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace BLL.Models
{
    public class ManageUser
    {
        //public string[] _UserFiles = { "Id", "Userid","UserPwd", "UserName", "UserState", "Roleid" };
        public string[] _UserFiles = { "id", "userid", "userpwd", "userName", "userState", "roleid" };
        
        /// <summary>
        /// 用户登陆验证
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool CheckUser(MODEL.MMUser model) {

            bool ret = false;
            DataTable dt=new DataTable();
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_sql = "select * from [tb_mc_admin] where Username='"+model.userName+"' and userpwd='"+model.userPwd+"'";
            try {

               dt=db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];

            }
            catch (Exception ex) { 
            
            }
            if(dt.Rows.Count>0){
                ret = true;
            }
            return ret;
        
        }
        public string DTree(string id) {

            string ret = string.Empty;

            return ret;
        }
        /// <summary>
        /// 获取管理员信息
        /// </summary>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public string Getinfo(string start,string limit) {

            string ret = string.Empty;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_sql = "  select top " + limit + " * from [tb_mc_admin] where id not in ( select top " + start + " id from [tb_mc_admin] order by id desc ) order by id desc  ";
            str_sql = start == "0" ? "  select top " + limit + " * from [tb_mc_admin] order by id desc" : str_sql;
            DataTable dt=new DataTable();
            string tcount = "0";
            try {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
                tcount = db.ExecuteScalar(COMM.PucConfig.ConnectionStringA, CommandType.Text, "select count(*) from [tb_mc_admin]").ToString();
            }
            catch (Exception ex) { }
            BLL.JSONHelper jsons = new BLL.JSONHelper();

        
 
             foreach(DataRow dr in dt.Rows){

                     foreach(string s in _UserFiles){
                     
                         jsons.AddItem(s,dr[s].ToString());
                     }
                     jsons.ItemOk();
             
             }


            jsons.totlalCount =Convert.ToInt32( tcount);
            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool Adduser(MODEL.MAdmin model) {
            bool ret=false;
            string str_sql = " insert into [tb_mc_admin] (userid, userpwd, userName, userState, roleid)values('"+model.Userid+"', '"+model.UserPwd+"', '"+model.UserName+"', '"+model.UserState.ToString()+"', "+model.RoleId.ToString()+") ";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try {

                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            
            return ret;
        }
        /// <summary>
        /// 根据id取得管理员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetUserinfoById(string id) {
            string ret = string.Empty;
            DataTable dt = new DataTable();
            string str_sql=" select * from [tb_mc_admin] where id= "+id;
            Library.SqlProvider.OleDb db=new Library.SqlProvider.OleDb();
            try{
                        dt=db.ExecuteDataset(COMM.PucConfig.ConnectionStringA,CommandType.Text,str_sql).Tables[0];
            }catch(Exception ex){}

 

            BLL.FormJSONHelper jsons = new BLL.FormJSONHelper();
            jsons.success = true;


            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in _UserFiles)
                {

                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }


 
            ret = jsons.ToString();
            //string str_sql="";
            //Library.SqlProvider.OleDb db=new Library.SqlProvider.OleDb();
            //try{
            //db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA,CommandType.Text,str_sql);
            //}catch(Exception ex){}
            return ret;
        
        }
        /// <summary>
        /// 编辑管理员信息
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool EditUser(MODEL.MAdmin model){
        
            bool ret=false;
            string str_sql=" update [tb_mc_admin] set userid='"+model.Userid+"', userpwd='"+model.UserPwd+"', userName='"+model.UserName+"', userState='"+model.UserState+"', roleid="+model.RoleId.ToString()+"  where id="+model.Id.ToString();
            Library.SqlProvider.OleDb db=new Library.SqlProvider.OleDb();
            try{
                            db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA,CommandType.Text,str_sql);
                ret=true;
            }catch(Exception ex){}
            return ret;
        }
        /// <summary>
        /// 删除管理员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Deltuser(string id){
            bool ret=false;
            string str_sql="delete from [tb_mc_admin] where id in("+id+")";
            Library.SqlProvider.OleDb db=new Library.SqlProvider.OleDb();
            try{
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA,CommandType.Text,str_sql);
                ret=true;
            }catch(Exception ex){}
            return ret;
        
        }
    }
}
